const db = require('./config/db');

// 创建用户管理表并插入示例数据的脚本
async function setupUsersTable() {
  try {
    // 创建表
    await db.execute(`
      CREATE TABLE IF NOT EXISTS user_manage (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(50) NOT NULL COMMENT '姓名',
        account VARCHAR(50) NOT NULL UNIQUE COMMENT '账号',
        amount DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '金额',
        role VARCHAR(50) NOT NULL COMMENT '角色',
        status VARCHAR(20) NOT NULL DEFAULT '主账号' COMMENT '状态',
        avatar VARCHAR(255) COMMENT '头像URL',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
    
    console.log('用户管理表创建成功');
    
    // 检查是否已有数据
    const [rows] = await db.execute('SELECT COUNT(*) as count FROM user_manage');
    if (rows[0].count === 0) {
      // 插入示例数据
      const sampleUsers = [
        ['姓名', '20245079266', 5000.00, '2023-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['姓名', '20266078944', 4000.00, '2022-165000', '主账号', 'https://www.keaitupian.cn/cjpic/frombd/2/253/2203692359/101708973.jpg'],
        ['陈音', '20280092835', 5000.00, '2028-155000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['陈子', '20266078604', 5000.00, '2024-156000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['张穹', '20280092866', 5000.00, '2023-156000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['太郎', '20266078289', 5000.00, '2022-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['那那', '20276076039', 5000.00, '2022-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['张穹', '20245079266', 5000.00, '2023-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['张穹', '20266078944', 4000.00, '2022-165000', '主账号', 'https://www.keaitupian.cn/cjpic/frombd/2/253/2203692359/101708973.jpg'],
        ['陈音', '20280092835', 5000.00, '2028-155000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['陈子', '20266078604', 5000.00, '2024-156000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['张穹', '20280092866', 5000.00, '2023-156000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['太郎', '20266078289', 5000.00, '2022-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3'],
        ['那那', '20276076039', 5000.00, '2022-168000', '主账号', 'https://ts3.tc.mm.bing.net/th/id/OIP-C._5jgAUzO8FRcGQPcTFWsEgAAAA?cb=12&rs=1&pid=ImgDetMain&o=7&rm=3']
      ];
      
      for (const user of sampleUsers) {
        await db.execute(
          'INSERT INTO user_manage (name, account, amount, role, status, avatar) VALUES (?, ?, ?, ?, ?, ?)',
          user
        );
      }
      
      console.log('示例用户数据插入成功');
    } else {
      console.log('用户表中已有数据，跳过示例数据插入');
    }
    
    console.log('用户管理表初始化完成');
    process.exit(0);
  } catch (error) {
    console.error('初始化用户管理表失败:', error);
    process.exit(1);
  }
}

setupUsersTable();